﻿using System;
using System.Collections;
using System.Configuration;

namespace Config
{
    public class QueueSection : ConfigurationSection
    {
        [ConfigurationProperty("", IsDefaultCollection = true)]
        [ConfigurationCollection(typeof(QueueSetting))]
        public QueueSettingsCollection Queues
        {
            get { return (QueueSettingsCollection)this[""]; }
        }
    }

    public sealed class QueueSetting : ConfigurationElement
    {
        private const string NameProperty = "name";
        private const string QueueIDProperty = "queueid";
        private const string ErrorQueueIDProperty = "errorqueueid";
        private const string CreateContactProperty = "createcontact";
        private const string MatchCaseTicketNumberProperty = "matchcaseticketnumber";
        private const string SendCaseMailProperty = "sendcasemail";
        private const string CaseMailFromProperty = "casemail-from";
        private const string CaseMailFromTypeProperty = "casemail-fromtype";
        private const string CaseMailNotifyProperty = "casemail-notify";
        private const string CaseMailSubjectProperty = "casemail-subject";
        private const string CaseMailTemplateProperty = "casemail-template";
        private const string CaseMailUseHtmlProperty = "casemail-usehtml";
        private const string SendUpdateMailProperty = "sendupdatemail";
        private const string UpdateMailFromProperty = "updatemail-from";
        private const string UpdateMailFromTypeProperty = "updatemail-fromtype";
        private const string UpdateMailSubjectProperty = "updatemail-subject";
        private const string UpdateMailTemplateProperty = "updatemail-template";
        private const string UpdateMailUseHtmlProperty = "updatemail-usehtml";
        private const string UpdateCaseDescriptionProperty = "updatecasedescription";
        private const string SendCaseNotificationProperty = "sendcasenotificationmail";
        private const string CaseNotificationFromProperty = "casenotification-from";
        private const string CaseNotificationToProperty = "casenotification-to";
        private const string CaseNotificationSubjectProperty = "casenotification-subject";
        private const string CaseNotificationTemplateProperty = "casenotification-template";
        private const string CaseNotificationUseHtmlProperty = "casenotification-usehtml";
        private const string CaseAttributesProperty = "caseattributes";
        private const string RemoveRepliesFromQueueProperty = "removerepliesfromqueue";
        private const string MapCaseToAccountProperty = "mapcasetoaccount";
        private const string SetResponsibleContactProperty = "setresponsiblecontact";
        private const string GetContactNameFromExchangeProperty = "getcontactnamefromexchange";
        private const string ExchangeUsernameProperty = "exchangeusername";
        private const string ExchangePasswordProperty = "exchangepassword";
        private const string ExchangeDomainProperty = "exchangedomain";
        private const string UpdateCaseTitleProperty = "updatecasetitle";
        private const string RouteEmailsFromExchangeProperty = "routeemailsfromexchange";
        private const string GetEmailHeadersFromExchangeProperty = "getemailheadersfromexchange";

        public QueueSetting()
        {
        }

        public QueueSetting(string elementName)
        {
            Name = elementName;
        }

        [ConfigurationProperty(NameProperty, IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)this[NameProperty]; }
            set { this[NameProperty] = value; }
        }

      
        [ConfigurationProperty(CreateContactProperty, DefaultValue = true)]
        public bool CreateContact
        {
            get { return (bool)this[CreateContactProperty]; }
            set { this[CreateContactProperty] = value; }
        }

        [ConfigurationProperty(SendCaseMailProperty, DefaultValue = true)]
        public bool SendCaseMail
        {
            get { return (bool)this[SendCaseMailProperty]; }
            set { this[SendCaseMailProperty] = value; }
        }

        [ConfigurationProperty(CaseMailFromProperty, IsRequired = false)]
        public string CaseMailFrom
        {
            get { return (string)this[CaseMailFromProperty]; }
            set { this[CaseMailFromProperty] = value; }
        }

        [ConfigurationProperty(CaseMailFromTypeProperty, IsRequired = false)]
        public string CaseMailFromType
        {
            get { return (string)this[CaseMailFromTypeProperty]; }
            set { this[CaseMailFromTypeProperty] = value; }
        }

        [ConfigurationProperty(CaseMailNotifyProperty, IsRequired = false)]
        public string CaseMailNotify
        {
            get { return (string)this[CaseMailNotifyProperty]; }
            set { this[CaseMailNotifyProperty] = value; }
        }

        [ConfigurationProperty(CaseMailSubjectProperty, IsRequired = false)]
        public string CaseMailSubject
        {
            get { return (string)this[CaseMailSubjectProperty]; }
            set { this[CaseMailSubjectProperty] = value; }
        }

        [ConfigurationProperty(CaseMailTemplateProperty, IsRequired = false)]
        public string CaseMailTemplate
        {
            get { return (string)this[CaseMailTemplateProperty]; }
            set { this[CaseMailTemplateProperty] = value; }
        }

        [ConfigurationProperty(CaseMailUseHtmlProperty, IsRequired = false, DefaultValue = true)]
        public bool CaseMailUseHtml
        {
            get { return (bool)this[CaseMailUseHtmlProperty]; }
            set { this[CaseMailUseHtmlProperty] = value; }
        }

        [ConfigurationProperty(SendUpdateMailProperty, DefaultValue = true)]
        public bool SendUpdateMail
        {
            get { return (bool)this[SendUpdateMailProperty]; }
            set { this[SendUpdateMailProperty] = value; }
        }

        [ConfigurationProperty(UpdateMailFromProperty, IsRequired = false)]
        public string UpdateMailFrom
        {
            get { return (string)this[UpdateMailFromProperty]; }
            set { this[UpdateMailFromProperty] = value; }
        }

        [ConfigurationProperty(UpdateMailFromTypeProperty, IsRequired = false)]
        public string UpdateMailFromType
        {
            get { return (string)this[UpdateMailFromTypeProperty]; }
            set { this[UpdateMailFromTypeProperty] = value; }
        }

        [ConfigurationProperty(UpdateMailSubjectProperty, IsRequired = false)]
        public string UpdateMailSubject
        {
            get { return (string)this[UpdateMailSubjectProperty]; }
            set { this[UpdateMailSubjectProperty] = value; }
        }

        [ConfigurationProperty(UpdateMailTemplateProperty, IsRequired = false)]
        public string UpdateMailTemplate
        {
            get { return (string)this[UpdateMailTemplateProperty]; }
            set { this[UpdateMailTemplateProperty] = value; }
        }

        [ConfigurationProperty(UpdateMailUseHtmlProperty, IsRequired = false, DefaultValue = true)]
        public bool UpdateMailUseHtml
        {
            get { return (bool)this[UpdateMailUseHtmlProperty]; }
            set { this[UpdateMailUseHtmlProperty] = value; }
        }

        [ConfigurationProperty(QueueIDProperty, IsRequired = true)]
        public string QueueID
        {
            get { return (string)this[QueueIDProperty]; }
            set { this[QueueIDProperty] = value; }
        }

        [ConfigurationProperty(ErrorQueueIDProperty, IsRequired = false)]
        public string ErrorQueueID
        {
            get { return (string)this[ErrorQueueIDProperty]; }
            set { this[ErrorQueueIDProperty] = value; }
        }

        [ConfigurationProperty(MatchCaseTicketNumberProperty, IsRequired = false, DefaultValue = false)]
        public bool MatchCaseTicketNumber
        {
            get { return (bool)this[MatchCaseTicketNumberProperty]; }
            set { this[MatchCaseTicketNumberProperty] = value; }
        }

        [ConfigurationProperty(UpdateCaseDescriptionProperty, DefaultValue = false)]
        public bool UpdateCaseDescription
        {
            get { return (bool)this[UpdateCaseDescriptionProperty]; }
            set { this[UpdateCaseDescriptionProperty] = value; }
        }

        [ConfigurationProperty(SendCaseNotificationProperty, DefaultValue = false)]
        public bool SendCaseNotification
        {
            get { return (bool)this[SendCaseNotificationProperty]; }
            set { this[SendCaseNotificationProperty] = value; }
        }

        [ConfigurationProperty(CaseNotificationFromProperty, IsRequired = false)]
        public string CaseNotificationFrom
        {
            get { return (string)this[CaseNotificationFromProperty]; }
            set { this[CaseNotificationFromProperty] = value; }
        }

        [ConfigurationProperty(CaseNotificationToProperty, IsRequired = false)]
        public string CaseNotificationTo
        {
            get { return (string)this[CaseNotificationToProperty]; }
            set { this[CaseNotificationToProperty] = value; }
        }

        [ConfigurationProperty(CaseNotificationSubjectProperty, IsRequired = false)]
        public string CaseNotificationSubject
        {
            get { return (string)this[CaseNotificationSubjectProperty]; }
            set { this[CaseNotificationSubjectProperty] = value; }
        }

        [ConfigurationProperty(CaseNotificationTemplateProperty, IsRequired = false)]
        public string CaseNotificationTemplate
        {
            get { return (string)this[CaseNotificationTemplateProperty]; }
            set { this[CaseNotificationTemplateProperty] = value; }
        }

        [ConfigurationProperty(CaseNotificationUseHtmlProperty, DefaultValue = false)]
        public bool CaseNotificationUseHtml
        {
            get { return (bool)this[CaseNotificationUseHtmlProperty]; }
            set { this[CaseNotificationUseHtmlProperty] = value; }
        }

        [ConfigurationProperty(CaseAttributesProperty, IsRequired = false)]
        public string CaseAttributes
        {
            get { return (string)this[CaseAttributesProperty]; }
            set { this[CaseAttributesProperty] = value; }
        }

        [ConfigurationProperty(RemoveRepliesFromQueueProperty, DefaultValue = false, IsRequired = false)]
        public bool RemoveRepliesFromQueue
        {
            get { return (bool)this[RemoveRepliesFromQueueProperty]; }
            set { this[RemoveRepliesFromQueueProperty] = value; }
        }

        [ConfigurationProperty(MapCaseToAccountProperty, DefaultValue = false, IsRequired = false)]
        public bool MapCaseToAccount
        {
            get { return (bool)this[MapCaseToAccountProperty]; }
            set { this[MapCaseToAccountProperty] = value; }
        }

        [ConfigurationProperty(SetResponsibleContactProperty, DefaultValue = false, IsRequired = false)]
        public bool SetResponsibleContact
        {
            get { return (bool)this[SetResponsibleContactProperty]; }
            set { this[SetResponsibleContactProperty] = value; }
        }

        [ConfigurationProperty(GetContactNameFromExchangeProperty, DefaultValue = false, IsRequired = false)]
        public bool GetContactNameFromExchange
        {
            get { return (bool)this[GetContactNameFromExchangeProperty]; }
            set { this[GetContactNameFromExchangeProperty] = value; }
        }

        [ConfigurationProperty(ExchangeUsernameProperty, IsRequired = false)]
        public string ExchangeUsername
        {
            get { return (string)this[ExchangeUsernameProperty]; }
            set { this[ExchangeUsernameProperty] = value; }
        }

        [ConfigurationProperty(ExchangePasswordProperty, IsRequired = false)]
        public string ExchangePassword
        {
            get { return (string)this[ExchangePasswordProperty]; }
            set { this[ExchangePasswordProperty] = value; }
        }

        [ConfigurationProperty(ExchangeDomainProperty, IsRequired = false)]
        public string ExchangeDomain
        {
            get { return (string)this[ExchangeDomainProperty]; }
            set { this[ExchangeDomainProperty] = value; }
        }

        [ConfigurationProperty(UpdateCaseTitleProperty, DefaultValue = false, IsRequired = false)]
        public bool UpdateCaseTitle
        {
            get { return (bool)this[UpdateCaseTitleProperty]; }
            set { this[UpdateCaseTitleProperty] = value; }
        }

        [ConfigurationProperty(RouteEmailsFromExchangeProperty, DefaultValue = false, IsRequired = false)]
        public bool RouteEmailsFromExchange
        {
            get { return (bool)this[RouteEmailsFromExchangeProperty]; }
            set { this[RouteEmailsFromExchangeProperty] = value; }
        }

        [ConfigurationProperty(GetEmailHeadersFromExchangeProperty, DefaultValue = false, IsRequired = false)]
        public bool GetEmailHeadersFromExchange
        {
            get { return (bool)this[GetEmailHeadersFromExchangeProperty]; }
            set { this[GetEmailHeadersFromExchangeProperty] = value; }
        }
        
    }

    public sealed class QueueSettingsCollection : ConfigurationElementCollection<QueueSetting>
    {

        public QueueSettingsCollection() : base(StringComparer.CurrentCultureIgnoreCase)
        {
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new QueueSetting();
        }

        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return new QueueSetting(elementName);
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((QueueSetting)element).Name;
        }
    }

    public abstract class ConfigurationElementCollection<T> : ConfigurationElementCollection where T : ConfigurationElement
    {

        protected ConfigurationElementCollection()
        {
        }

        protected ConfigurationElementCollection(IComparer comparer) : base(comparer)
        {
        }

        public void Add(T settings)
        {
            BaseAdd(settings);
        }

        public void Clear()
        {
            BaseClear();
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Remove(T settings)
        {
            BaseRemove(GetElementKey(settings));
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public int IndexOf(T settings)
        {
            return BaseIndexOf(settings);
        }

        protected override void BaseAdd(int index, ConfigurationElement element)
        {
            base.BaseAdd(index, element);
        }
    }
}
